package com.tianqingwl.gsystem.content.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tianqingwl.gsystem.common.vo.CommonResultVo;
import com.tianqingwl.gsystem.common.vo.PageResultVo;
import com.tianqingwl.gsystem.content.pojo.ContentType;
import com.tianqingwl.gsystem.content.vo.ContentTypeVo;
import com.tianqingwl.gsystem.content.service.ContentTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Controller
@RequestMapping("/manage/content-type")
@PreAuthorize("@gs.check('content_type')")
public class ContentTypeController {

    @Autowired
    private ContentTypeService contentTypeService;

    @GetMapping("/content-type-manage.html")
    public String contentTypePage() {
        return "manage/content/content-type-manage";
    }

    @GetMapping("/content-type-edit.html")
    public String contentTypeEdit(Model model, Long typeId) {
        if (typeId != null) {
            ContentType contentType = contentTypeService.getContentTypeById(typeId);
            if (contentType != null) {
                //父级
                if (contentType.getParentId() != null) {
                    ContentType parentContentType = contentTypeService.getContentTypeById(contentType.getParentId());
                    model.addAttribute("parentTypeName", parentContentType != null ? parentContentType.getTypeName() : null);
                }
                model.addAttribute("contentType", contentType);
            }
        }

        return "manage/content/content-type-edit";
    }

    @GetMapping("/list")
    @ResponseBody
    public Object list(Long page, Long limit, String typeName) {
        page = page == null ? 1 : page;
        limit = limit == null ? 10 : limit;

        Page<ContentType> contentTypePage = new Page<>(page, limit);
        contentTypeService.listContentTypePage(contentTypePage, typeName, 1,null);

        //bean->vo转换
        List<ContentTypeVo> voList = new ArrayList<>();
        List<ContentType> contentTypeList = contentTypePage.getRecords();
        for (ContentType contentType : contentTypeList) {
            ContentTypeVo vo = new ContentTypeVo();
            vo.setParentId(contentType.getParentId());
            vo.setTypeId(contentType.getTypeId());
            vo.setTypeName(contentType.getTypeName());
            vo.setDescription(contentType.getDescription());
            vo.setKeywords(contentType.getKeywords());
            vo.setOrderNum(contentType.getOrderNum());
            vo.setTitle(contentType.getTitle());
            voList.add(vo);
        }
        return PageResultVo.success(contentTypePage.getTotal(), contentTypePage.getPages(), voList);
    }

    @PostMapping({"/typeSave"})
    @ResponseBody
    public Object typeSave(ContentType contentType) {
        if (contentType == null) {
            return CommonResultVo.fail("未传入数据");
        }
        try {
            //唯一性校验
            boolean exists = contentTypeService.checkNameExists(contentType);
            if (exists) {
                return CommonResultVo.fail("类别名称不能重复");
            }
            ContentType saveResult = contentTypeService.save(contentType);
            return CommonResultVo.success(saveResult);
        } catch (Exception e) {
            return CommonResultVo.fail(e.getMessage());
        }
    }

    @GetMapping("/delete")
    @ResponseBody
    public Object delete(Long[] contentTypeIds, Long contentTypeId) {
        try {
            Integer type = 1;
            if (contentTypeId != null) {
                //有内容
                if (contentTypeService.haveContent(Arrays.asList(contentTypeId))) {
                    return CommonResultVo.fail("类别下还有文章，不可删除！");
                }
                //有子级
                if (contentTypeService.haveChild(Arrays.asList(contentTypeId))) {
                    return CommonResultVo.fail("此类别还有子类别，不可删除！");
                }
                contentTypeService.deleteById(contentTypeId, type);
            } else if (contentTypeIds != null && contentTypeIds.length > 0) {
                if (contentTypeService.haveContent(Arrays.asList(contentTypeIds))) {
                    return CommonResultVo.fail("类别下还有文章，不可删除！");
                }
                //有子级
                if (contentTypeService.haveChild(Arrays.asList(contentTypeIds))) {
                    return CommonResultVo.fail("此类别还有子类别，不可删除！");
                }
                contentTypeService.deleteByIds(Arrays.asList(contentTypeIds), type);
            } else {
                return CommonResultVo.fail("no id");
            }
            return CommonResultVo.success();
        } catch (Exception e) {
            return CommonResultVo.fail(e.getMessage());
        }
    }
}
